package com.ql.controller.doctor;
import com.ql.pojo.user.Role;
import com.ql.pojo.yk.Doctor;
import com.ql.service.doctor.DoctorService;
import com.ql.service.keshi.KeShiService;
import com.ql.service.role.RoleService;
import com.ql.utils.Constrants;
import com.ql.utils.DateUtils;
import com.ql.utils.PageHelper;
import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("/webapp/")
public class DoctorController {
    @Resource
    private DoctorService doctorService;
    @Resource
    private RoleService roleService;
    @Resource
    private KeShiService keShiService;

    @RequestMapping(value = "yuangong")
//    ,produces = "application/json;charset=utf-8"
    /*@ResponseBody*/
    public String getDoctorList(@RequestParam(value = "queryName",required = false) String queryName,
                                @RequestParam(value = "queryDoctorjobTitle",required = false)String queryDoctorjobTitle,
            Model model,PageHelper ph){

        //add:查询中带入分页条件:currentPageNo page-size
       if(null == queryDoctorjobTitle){
           queryDoctorjobTitle="0";
        }
        //分页查询员工信息
        List<Doctor> doctorList = doctorService.getAllDoctor(ph,queryName,Integer.parseInt(queryDoctorjobTitle));

        model.addAttribute("doctorList",doctorList);
        model.addAttribute("currentPageNo",ph.getCurrentPageNo());
        model.addAttribute("totalCount",ph.getTotalCount());
        model.addAttribute("totalPageCount",ph.getTotalPageCount());
        //加入所有的角色
        model.addAttribute("roleList",roleService.getAllRole());
        model.addAttribute("keshiList",keShiService.getAllKeShi());
        //把查询条件带到页面
        model.addAttribute("queryName",queryName);
        model.addAttribute("queryDoctorjobTitle",queryDoctorjobTitle);
        return "yuangong";
    }

    //查看医生信息
    @RequestMapping("seedoctor")
    public String getSeeDoc(@RequestParam(value = "docId",required = false) String docId,Model model){
        Doctor seedoc = doctorService.getDoctorById(docId);
        model.addAttribute("doctor",seedoc);
        return "seedoctor";
    }

    /*新增*/
    @RequestMapping("add")
    public String add(HttpSession session,
                      Model model){
        if (null == session.getAttribute(Constrants.USER_SESSION)){
            model.addAttribute("roleList",roleService.getAllRole());
            model.addAttribute("keshiList",keShiService.getAllKeShi());
            return "doctorAdd";
        }
        return "doctorAdd";

    }

    /**
     * 增加doctor信息，保存到数据库
     * @param doctor
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("addDoctor")
    public String addDoctor(@ModelAttribute("doctor")Doctor doctor,String entryDate, HttpSession session,
                            Model model)throws Exception{
        /*doctor.setEntrytime(DateUtils.parse(entryDate));*/
        if (null == session.getAttribute(Constrants.USER_SESSION)){
            //调用增加方法
            if (doctorService.addNewDoctor(doctor)){
                return "forward:yuangong";
            }else {
                return "doctorAdd";
            }
        }
        return "doctorAdd";
    }


    /*进入修改页面*/
    @RequestMapping(value = "modifyById")
    private String modify(@RequestParam("docId") String doctorId,Model model){
        System.out.println(doctorId);
        //跳转到修改页面
        model.addAttribute("doctor",doctorService.getDoctorById(doctorId));
        model.addAttribute("roleList",roleService.getAllRole());
        model.addAttribute("keshiList",keShiService.getAllKeShi());
        //调用service根据id查询用户信息
        return "modifyDoctor";
    }
    /*
    * 根据提交的修改信息，进行修改
    * */
    @RequestMapping("modify")
    public String modify(@ModelAttribute("doctor")Doctor doctor,
                         @RequestParam("departureDate")String departureDate, HttpSession session,
                            Model model){
        departureDate = departureDate +" "+ "00:00:00";

        try {
            doctor.setDeparturetime(DateUtils.parse(departureDate));
        } catch (Exception e) {
            System.out.println("DoctorController。modify转换日期异常"+e.getMessage());
        }
        if (null == session.getAttribute(Constrants.USER_SESSION)){
            //调用增加方法
            if (doctorService.updateDoctor(doctor)){
                return "forward:yuangong";
            }else {
                return "modifyDoctor";
            }
        }
        return "modifyDoctor";
    }
}
